package de.koller.aframe.plain.gui.component;

import de.koller.aframe.util.misc.ABoundingBox;
import java.awt.Graphics2D;
import java.util.HashSet;
import java.util.Set;

public class Component {
	
	private boolean repaint = true;
	private final Set<Component> childs = new HashSet<>();
	private Component mother = null;
	
	public final ABoundingBox bounds = new ABoundingBox();
	
	public void renderThis( Graphics2D g ) {
		if( repaint == true ) {
			drawThis( g );
			repaint = false;
		}
		
		for( Component c : childs )
			c.renderThis( g );
	}
	
	protected void drawThis( Graphics2D g ) {
		
	}
	
	public void addComponent( Component comp ) {
		childs.add( comp );
		comp.mother = this;
	}
	
	public void removeComponent( Component comp ) {
		childs.remove( comp );
		comp.mother = null;
	}
	
	public void repaint() {
		repaint = true;
	}
	
	public ABoundingBox getFrameBounds() {
		if( mother == null )
			return bounds;
		
		ABoundingBox b = mother.bounds;
		return new ABoundingBox( bounds.x + b.x, bounds.y + b.y, bounds.w, bounds.h );
	}
	
}
